﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace AutoParkingManagement
{
    public partial class AddDeleteCard : Form
    {
        #region Private members
        private string _cardID = String.Empty;
        private bool _isNew;        
        #endregion

        #region Properties
        #endregion

        #region Public members
        public delegate void CardAddedRemovedHandler(Guid cardTypeID, string cardID, bool VIP);
        public event CardAddedRemovedHandler CardAddedRemoved;

        public Infragistics.Win.Misc.UltraLabel LabelError = null;

        public void StopReadCard()
        {   
            this.Close();
            this.Dispose();
        }
        #endregion

        #region Events
        private void AddDeleteCard_Load(object sender, EventArgs e)
        {
            timer1.Interval = AppConfig.CardReaderTimer;
            timer1.Enabled = true;            
            LabelError = lblError;
            _cardID = String.Empty;            
            lblCardType.Visible = _isNew;
            cboCardType.Visible = _isNew;
            chkVIP.Visible = _isNew;
            if (_isNew)
            {
                this.Text = "Thêm mới thẻ xe";
                this.Height = 180;
                cboCardType.DataSource = Common.GetCardTypes(false);
                cboCardType.DisplayMember = "Name";
                cboCardType.ValueMember = "CardTypeID";
                if (cboCardType.Items.Count>0)
                {
                    cboCardType.SelectedIndex = 0;
                }
            }
            else
            {
                cboCardType.DataSource = null;
                this.Text = "Hủy bỏ thẻ xe";                
                this.Height = 130;
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            try
            {
                timer1.Enabled = false;
                lblError.Text = String.Empty;                               
                btnStart.Enabled = false;
                string strCardID = String.Empty;
                string strMessage=String.Empty;
                if (AppConfig.HasCardReader == "0")
                {
                    strCardID = Guid.NewGuid().ToString();                    
                }
                else
                {

                    if (!DLL1356.GetCardIDByCardReader(AppConfig.RFIDRIGHT, ref strCardID, ref strMessage))
                    {
                        //MessageBox.Show(strMessage);
                        return;
                    }
                }

                if (strCardID == _cardID)
                {
                    if (_isNew)
                    {
                        lblError.Text = "Thẻ này đã nạp rồi.";
                    }
                    else
                    {
                        lblError.Text = "Thẻ này đã xóa.";
                    }
                    
                    return;
                }                

                _cardID = strCardID;

                txtCardID.Text = strCardID;

                if (_isNew)
                {
                    if (cboCardType.SelectedIndex>=0)
                    {
                        CardAddedRemoved((Guid)cboCardType.Value, strCardID, chkVIP.Checked);
                    }
                }
                else
                {
                    CardAddedRemoved(Guid.Empty, txtCardID.Text, chkVIP.Checked);
                }
            }
            finally
            {                
                btnStart.Enabled = true;
                timer1.Enabled = true;
            }
        }

        private void AddDeleteCard_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {                
                this.Close();
                this.Dispose();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            btnStart_Click(null, null); 
        }
        #endregion

        #region Constructros
        public AddDeleteCard(bool isNew)
        {
            InitializeComponent();
            _isNew = isNew;
            CheckForIllegalCrossThreadCalls = false;
        }
        #endregion
        
    }
}
